<?php

namespace App\Models;


use App\Http\Common\Lib\Common;

class Menu extends BaseModel
{
	//
	protected $validatorName = 'Menu';

	/**
	 * 角色列表
	 * @param int $parentId
	 * @param array $where
	 * @param bool $needSub
	 * @param array $lists
	 * @return array
	 */
	public function getList($parentId = 0, $where = [], $needSub = true, &$lists = [])
	{
		$res = $this->where('parent_id', $parentId)
			->where(function ($query) use ($where) {
				if (!empty($where)) {
					$query->where($where);
				}
			})->orderBy('is_menu', 'desc')
			->orderBy('is_checked', 'desc')
			->orderBy('sort_no', 'asc')
			->get();
		if ($res) {
			foreach ($res as $key => $value) {
				//array_push($lists,$res);
				$lists[] = $value;
				if ($needSub) {
					$this->getList($value->id, $where, $needSub, $lists);
				}
			}
		}
		return $lists;
	}

	/**
	 * 列表，主要用于下拉列表
	 * @param int $parentId
	 * @param bool $needSub
	 * @param int $loop
	 * @param array $lists
	 * @return array
	 */
	public function selectList($parentId = 0, $needSub = true, $loop = 0, &$lists = [])
	{
		$res = $this->where('parent_id', $parentId)
			->where('status', 1)
			->select(['id', 'title'])
			//->toSql();
			->get();
		//dd($res);
		if ($res) {
			foreach ($res as $key => $value) {
				$value->title = str_repeat('&nbsp;&nbsp;', $loop) . $value->title;
				$lists[] = $value;
				if ($needSub) {
					$this->selectList($value->id, $needSub, $loop + 1, $lists);
				}
			}
		}
		return $lists;
	}

	public function add($input)
	{
		$validateRes = $this->validateData($input, 'add');
		if ($validateRes['status'] != config('statusInfo.status.success')) {
			return Common::dataFormat($validateRes['status'], $validateRes['msg']);
		}
		//检查url是否重复
		if (isset($input['url']) && $input['url']) {
			$isUnique = $this->checkUnique('url', $input['url'], [['status', '>', -1], ['method', '=', $input['method']]]);
			if (!$isUnique) {
				return Common::dataFormat(config('statusInfo.status.repeated'), '该菜单已存在');
			}
		} else {
			$input['url'] = '';
		}
		$menu = new Menu();
		$menu = $menu->setData($input);
		$res = $menu->save();
		//$id = $this->insertGetId($input);
		if ($res) {
			return Common::dataFormat(config('statusInfo.status.success'), config('statusInfo.msg.success'), [$this->primaryKey => $menu->id]);
		} else {
			return Common::dataFormat(config('statusInfo.status.fail'), config('statusInfo.msg.fail'));
		}
	}

	public function edit($input)
	{
		$validateRes = $this->validateData($input, 'edit');
		if ($validateRes['status'] != config('statusInfo.status.success')) {
			return Common::dataFormat($validateRes['status'], $validateRes['msg']);
		}
		//检查url是否重复
		if (isset($input['url']) && $input['url']) {
			$isUnique = $this->checkUnique('url', $input['url'], [['status', '>', -1], ['method', '=', $input['method']], ['id', '<>', $input['id']]]);
			if (!$isUnique) {
				return Common::dataFormat(config('statusInfo.status.repeated'), '该菜单已存在');
			}
		} else {
			$input['url'] = '';
		}
		$item = $this->where($this->primaryKey, $input[$this->primaryKey])->where('status', '>', -1)->first();
		if (!$item) {
			return Common::dataFormat(config('statusInfo.status.notFound'), config('statusInfo.msg.notFound'));
		}
		$subIds = $this->getSubIds($input[$this->primaryKey], [['status', '>', -1]]);
		$subIds[] = $input[$this->primaryKey];
		if (in_array($input['parent_id'], $subIds)) {
			return Common::dataFormat(config('statusInfo.status.fail'), '父级分类不知能是自身或者自身的下级分类');
		}
		$res = $this->where($this->primaryKey, $input[$this->primaryKey])->update($input);
		if ($res) {
			return Common::dataFormat(config('statusInfo.status.success'), config('statusInfo.msg.success'));
		} else {
			return Common::dataFormat(config('statusInfo.status.fail'), config('statusInfo.msg.fail'));
		}
	}

	/**
	 * 树状图
	 * @param int $parentId
	 * @param array $where
	 * @param bool $needSub
	 * @param array $checkedIds
	 * @return array
	 */
	public function treeList($parentId = 0, $where = [], $needSub = true, $checkedIds = [])
	{
		$list = [];
		$res = $this->where('parent_id', $parentId)
			->where(function ($query) use ($where) {
				if (!empty($where)) {
					$query->where($where);
				}
			})->select([$this->primaryKey, 'parent_id', 'title'])
			->orderBy('is_menu', 'desc')
			->orderBy('is_checked', 'desc')
			->orderBy('sort_no', 'desc')
			->get();
		if ($res) {
			foreach ($res as $key => $value) {
				$item = [
					'id' => $value->id,
					'label' => $value->title,
				];
				if (in_array($value->id, $checkedIds)) {
					$item['checked'] = true;
				}
				if ($needSub) {
					$subs = $this->treeList($value->id, $where, $needSub, $checkedIds);
					if ($subs) {
						$item['children'] = $subs;
					}
				}
				$list[] = $item;
			}
		}
		return $list;
	}

	/**
	 * 获取侧边菜单
	 * @param int $parentId
	 * @param bool $needSub
	 * @param bool $isAll
	 * @param array $allowIds
	 * @return array
	 */
	public function getSideMenus($parentId = 0, $needSub = true, $isAll = false, $allowIds = [])
	{
		$menus = [];
		$res = $this->where('parent_id', $parentId)
			->where('status', 1)
			->where('is_menu', 1)
			->orderBy('sort_no', 'desc')
			->select(['id', 'title', 'url'])
			->get();
		if ($res) {
			foreach ($res as $key => $value) {
				if (!$isAll && !in_array($value->id, $allowIds)) {
					continue;
				}
				$item = [
					'id' => $value->id,
					'title' => $value->title,
					'url' => $value->url,
				];
				if ($needSub) {
					$subs = $this->getSideMenus($value->id, $needSub, $isAll, $allowIds);
					if ($subs) {
						$item['subMenus'] = $subs;
						unset($subs);
					}
				}
				$menus[] = $item;
			}
		}
		return $menus;
	}
}
